package com.java1234.web;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.java1234.dao.MessageDao;
import com.java1234.model.Message;
import com.java1234.util.DbUtil;
import com.java1234.util.ResponseUtil;
import com.java1234.util.StringUtil;

import net.sf.json.JSONObject;

public class MessageSaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	DbUtil dbUtil=new DbUtil();
	MessageDao messageDao=new MessageDao();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		//添加信息
		String date=request.getParameter("date");
		String cid=request.getParameter("cid");
		String name=request.getParameter("name");
		String m_desc=request.getParameter("m_desc");
		String choose=request.getParameter("choose");
		String form=request.getParameter("form");
		String head=request.getParameter("head");
		String position=request.getParameter("position");
		String workspace=request.getParameter("workspace");
		String teleph=request.getParameter("teleph");
		String members=request.getParameter("members");
		String aid=request.getParameter("aid");
		//修改id
		String id=request.getParameter("id");
		Message message=new Message(date, cid, choose, name, form, head, position, workspace, teleph, members, m_desc, null, null, null,aid);
		if(StringUtil.isNotEmpty(id)){
			message.setId(Integer.parseInt(id));
		}
		Connection con=null;
		try{
			con=dbUtil.getCon();
			int saveNums=0;
			JSONObject result=new JSONObject();
			if(StringUtil.isNotEmpty(id)){
				saveNums=messageDao.messageModify(con, message);
			}else{
				saveNums=messageDao.messageAdd(con, message);
			}
			if(saveNums>0){
				result.put("success", "true");
			}else{
				result.put("success", "true");
				result.put("errorMsg", "添加失败");
			}
			ResponseUtil.write(response, result);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
